package com.huakang.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huakang.entity.Inpatient;
import com.huakang.entity.Seeadoctor;
import com.huakang.entity.Sickbedroom;
import com.huakang.mapper.InpatientMapper;
import com.huakang.service.InpatientService;
import com.huakang.service.SeeadoctorService;
import com.huakang.service.SickbedroomService;
import com.huakang.utils.InpatientVo;
import com.huakang.utils.ResultVo;
import com.huakang.utils.SeeadoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.SortedMap;

@RestController
@RequestMapping("/inpatient")
public class InpatientController {
    @Autowired
    InpatientService inpatientService;
    @Autowired
    SickbedroomService sickbedroomService;
    @Autowired
    SeeadoctorService seeadoctorService;
    @PostMapping("/insert")
    public ResultVo insert(@RequestBody InpatientVo inpatientVo){
        Inpatient inpatient=new Inpatient(null,inpatientVo.getInpdate(),inpatientVo.getSrid(),inpatientVo.getKid(),inpatientVo.getPid(),inpatientVo.getDid(),inpatientVo.getBid());
        boolean b=inpatientService.insert(inpatient);
        boolean b2=sickbedroomService.insert(new Sickbedroom(null,inpatientVo.getBid(),inpatientVo.getSrid()));
        boolean b3=seeadoctorService.updateInhosp(inpatientVo.getSid());
        if(b&b2&b3){
            return ResultVo.success("成功");
        }else{
            return ResultVo.fail("失败");
        }
    }
    @GetMapping("/getAll")
    public  ResultVo getAll(@RequestParam(defaultValue = "1")Integer pageNum , @RequestParam(defaultValue = "5")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<InpatientVo> list=inpatientService.getAll();
        PageInfo<InpatientVo> pageInfo=new PageInfo<>(list);
        return  ResultVo.success("查询成功",pageInfo);
    }

    @GetMapping("/getSearch")
    public  ResultVo getSearch(@RequestParam(defaultValue = "1")Integer pageNum , @RequestParam(defaultValue = "5")Integer pageSize,String name){
        PageHelper.startPage(pageNum,pageSize);
        List<InpatientVo> list=inpatientService.getSearch(name);
        PageInfo<InpatientVo> pageInfo=new PageInfo<>(list);
        return  ResultVo.success("查询成功",pageInfo);
    }
    @GetMapping("/getInpid")
    public  ResultVo getInpid(Integer inpid, HttpSession session){
        InpatientVo inpatientVo=inpatientService.getInpid(inpid);
        session.setAttribute("inpatientVo",inpatientVo);
        return  ResultVo.success("成功");
    }
    @PostMapping("/getPidUpdate")
    public  ResultVo getPid(@RequestBody InpatientVo inpatientVo){
        Inpatient inpatient=inpatientService.getPid(inpatientVo.getPid());
        if(inpatient==null){
            return insert(inpatientVo);
        }
        remo(inpatient);
        return insert(inpatientVo);
    }
    @PostMapping("/remo")
    public  ResultVo remo( @RequestBody Inpatient inpatient){
        boolean b=inpatientService.remo(inpatient.getInpid());
        List<Seeadoctor> list = seeadoctorService.findPidOne(inpatient.getPid());
        Seeadoctor seeadoctor=list.get(0);
        seeadoctor.setInhosp("已出院");
        boolean b2=seeadoctorService.update(seeadoctor);
        boolean b3=sickbedroomService.remo(inpatient.getSrid(),inpatient.getBid());
        if(b && b2 && b3){
            return  ResultVo.success("成功");
        }
        return  ResultVo.fail("失败");
    }
    @PostMapping("/update")
    public ResultVo update(@RequestBody Inpatient inpatient){
        boolean b=inpatientService.update(inpatient);
        if(b){
            return ResultVo.success("成功");
        }
        return  ResultVo.fail("失败");
    }
}
